package com.zsd.android.complier;

import com.google.auto.service.AutoService;
import com.zsd.android.annotation.PermissionCheck;
import com.zsd.android.complier.utils.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

@AutoService(Processor.class)
public class PermissionProcessor extends AbstractProcessor {

    private Elements elementUtil;
    private Types typeUtil;
    private Filer filer;
    private Messager message;

    private HashMap<String, ArrayList<Element>> elementMap;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        elementUtil = processingEnvironment.getElementUtils();
        typeUtil = processingEnvironment.getTypeUtils();
        filer = processingEnvironment.getFiler();
        message = processingEnvironment.getMessager();
        elementMap = new HashMap<>();
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> mSet = new LinkedHashSet<>();
        mSet.add(PermissionCheck.class.getCanonicalName());
        return mSet;
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latest();
    }

    @Override
    public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        if (!TextUtils.isEmpty(set)) {
            Set<? extends Element> permissionCheckElements = roundEnvironment.getElementsAnnotatedWith(PermissionCheck.class);
            parseElements(permissionCheckElements);
            createAptFile();
            return true;
        }
        return false;
    }

    private void parseElements(Set<? extends Element> permissionCheckElements) {
        if (TextUtils.isEmpty(permissionCheckElements)) {
            return;
        }
        for (Element element : permissionCheckElements) {
//            ExecutableElement methodElement = (ExecutableElement) element;
            //获取包名
            String packageName = elementUtil.getPackageOf(element).getQualifiedName().toString();
            //获取方法名
            String methodName = element.getSimpleName().toString();
            ArrayList<Element> elements = elementMap.get(packageName + "$$Permission");
            if (TextUtils.isEmpty(elements)) {
                elements = new ArrayList<>();
                elementMap.put(packageName + "$$Permission", elements);
            }
            elements.add(element);
        }
    }

    private void createAptFile() {
        if (TextUtils.isEmpty(elementMap)) {
            return;
        }
        for (Map.Entry<String, ArrayList<Element>> entry : elementMap.entrySet()) {
            String packageName = entry.getKey();
            ArrayList<Element> elements = entry.getValue();
            for (Element element : elements) {
                PermissionCheck permissionCheck = element.getAnnotation(PermissionCheck.class);
                String[] permissions = permissionCheck.permissions();
                int requestCode = permissionCheck.requestCode();
            }
        }
    }
}
